![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
@types/debounce-promise
Advanced tools
TypeScript definitions for debounce-promise
@types/debounce-promise provides TypeScript type definitions for the debounce-promise package, which is used to debounce promises. Debouncing is a technique to limit the rate at which a function is executed, ensuring that it is only called once within a specified time frame, even if it is triggered multiple times.
Basic Debouncing
This feature allows you to debounce a promise-returning function. In this example, the fetchData function is debounced with a delay of 2000 milliseconds. Even if debouncedFetchData is called multiple times, the fetchData function will only be executed once within the 2-second window.
const debounce = require('debounce-promise');
const fetchData = () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve('Data fetched');
}, 1000);
});
};
const debouncedFetchData = debounce(fetchData, 2000);
debouncedFetchData().then(console.log); // 'Data fetched' after 2 seconds
Immediate Execution
This feature allows you to execute the debounced function immediately on the leading edge of the timeout. In this example, the fetchData function is executed immediately when debouncedFetchData is called, and subsequent calls within the 2-second window will be ignored.
const debounce = require('debounce-promise');
const fetchData = () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve('Data fetched');
}, 1000);
});
};
const debouncedFetchData = debounce(fetchData, 2000, { leading: true });
debouncedFetchData().then(console.log); // 'Data fetched' immediately
Cancel Debounced Function
This feature allows you to cancel a debounced function before it gets executed. In this example, the debouncedFetchData function is canceled before it can execute the fetchData function.
const debounce = require('debounce-promise');
const fetchData = () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve('Data fetched');
}, 1000);
});
};
const debouncedFetchData = debounce(fetchData, 2000);
debouncedFetchData().then(console.log); // 'Data fetched' after 2 seconds
debouncedFetchData.cancel(); // Cancels the debounced function
Lodash is a popular utility library that provides a wide range of functions for common programming tasks, including debouncing. The debounce function in Lodash can be used to debounce any function, not just promises. It offers more configuration options compared to debounce-promise.
Underscore is another utility library similar to Lodash, providing a variety of functional programming helpers. It also includes a debounce function that can be used to limit the rate at which a function is executed. Like Lodash, it is not limited to debouncing promises.
p-debounce is a small utility specifically designed for debouncing promise-returning functions. It is similar to debounce-promise but offers a simpler API and is focused solely on debouncing promises.
npm install --save @types/debounce-promise
This package contains type definitions for debounce-promise (https://github.com/bjoerge/debounce-promise).
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/debounce-promise.
declare namespace debounce {
interface DebounceOptions {
leading?: boolean | undefined;
accumulate?: boolean | undefined;
}
}
// func is called with an array of array of parameters if accumulate is true
// Use Array<[arg0, arg1, ..., argN]> as func's first parameter type for correct hints
declare function debounce<T extends any[], R>(
func: (args: Array<[...T]>) => R,
wait?: number,
options?: debounce.DebounceOptions & { accumulate: true },
): (
...args: T
) => R extends Promise<any> ? R
: Promise<R>;
declare function debounce<T extends (...args: any[]) => any>(
func: T,
wait?: number | (() => number),
options?: debounce.DebounceOptions,
): (
...args: Parameters<T>
) => ReturnType<T> extends Promise<any> ? ReturnType<T>
: Promise<ReturnType<T>>;
export = debounce;
These definitions were written by Wu Haotian, and Trevor Robinson.
FAQs
TypeScript definitions for debounce-promise
We found that @types/debounce-promise demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.